package dept

import (
	"context"
	"go-zero-project/services/user/rpc/pb"

	"go-zero-project/services/user/api/internal/svc"
	"go-zero-project/services/user/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type InfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewDeptInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *InfoLogic {
	return &InfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *InfoLogic) DeptInfo(req *types.DeptInfoReq) (resp *types.DeptInfoResp, err error) {
	// 1.builder rpc request
	infoReq := pb.DeptInfoReq{DeptId: req.DeptId}
	// 2.request rpc
	deptInfo, err := l.svcCtx.UserCenterClient.DeptInfo(l.ctx, &infoReq)
	if err != nil {
		return nil, err
	}
	// 3.return obj
	resp = new(types.DeptInfoResp)
	resp.Name = deptInfo.Name
	resp.ParentId = deptInfo.ParentId
	resp.ParentName = deptInfo.ParentName
	resp.OrderNum = int(deptInfo.OrderNum)
	resp.SubNodeNum = int(deptInfo.SubNodeNum)
	resp.DeptId = deptInfo.DeptId
	return resp, nil
}
